<?php
/**
 * DolphinGTK - phpGtk framework
 *
 * Графический модуль
 *
 * @package    DolphinGTK
 * @author     Askarov Ruslan
 * @copyright  Askarov Ruslan (c) 2010.
 * @link       http://groups.google.com/group/dolphingtk
 * @license    http://www.opensource.org/licenses/gpl-license.php GPL
 * @version    0.1
 * @since      PHP v.: 5.2.1
 * @subpackage desktop
 * @category   module
 * @filesource
 */

class Browser
{
  private static $table;
  private static $path;


  public function set()
  {
    self::$path = OS::get_profile();

    self::$table = new Table;
    self::$table->row_color('#dddddd', '#ffffff');
    self::$table->sortable(true);

    self::$table->create(
      array('icon',   '#' ),
      array('string', i18n('File name') ),
      array('string', i18n('Ext') ),
      array('string', i18n('Size') ),
      array('string', i18n('Changed') )
    );

    self::$table->on_key(
      array('Browser','on_key')
    );

    self::$table->on_bottom(
      array('Browser','on_key')
    );


    self::opendir('');

    return self::$table->get(600);
  }


  public static function opendir( $filename = '' )
  {
    self::$table->clear();
    
    $filename    = ($filename) ? self::$path.$filename.DIRECTORY_SEPARATOR : self::$path;
    chdir($filename);
    self::$path  = getcwd() . DIRECTORY_SEPARATOR;
    $list = array();
    $type = array();
    $type['file'] = array();
    $list['file'] = array();
    $type['directory'] = array();
    $list['directory'] = array();

    if( is_dir( $filename ) ):
    $i = 0;

    $dir = opendir($filename);
    
    while(false !== ($file = readdir($dir)))
    {
      $len = strlen($file);
      if( $file != '.' && substr($file,0,1) != '.' && @substr($file,($len-1),$len) != '~' or $file == '..' )
      {
        $ffile   = $filename.DIRECTORY_SEPARATOR.$file;
        $key = (is_dir($ffile)) ? 'directory' : 'file';

        $type[$key][] = $file;
        
        $list[$key][$i][] = $key;
        $list[$key][$i][] = $file;

        $ext = explode('.',$file);

        if( $key != 'directory' && count($ext) > 1 ) {
          $ext = end($ext);
          $list[$key][$i][1] = str_replace(".$ext",'',$file);
        } else {
          $ext = '';
        }

        $list[$key][$i][] = $ext;

        if( $key != 'directory' ) {
          $list[$key][$i][] = self::formatBytes(filesize($ffile));
        } else {
          $list[$key][$i][] = i18n('DIR');
        }

        
        $list[$key][$i][] = date("d-m-Y H:i", filemtime($ffile));
        $i ++;
      }
    }

    closedir($dir);

    if( is_array($type['directory']) && is_array($list['directory']) ) {
      array_multisort($type['directory'], SORT_ASC, $list['directory']);
    }

    if( count($type['file']) > 0 && count($list['file']) > 0 ) {
      array_multisort($type['file'], SORT_ASC, $list['file']);
    }


    if( count($list['directory']) > 0 && count($list['file']) > 0 ) {
      $data = array_merge( $list['directory'], $list['file'] );
    }
    elseif( !count($list['directory']) && count($list['file']) > 0 ) {
      $data = $list['file'];
    }
    elseif( count($list['directory']) > 0 && !count($list['file']) ) {
      $data = $list['directory'];
    }
    else {
      $data = array();
    }

    

    foreach( $data as $value )
    {
      self::$table->insert($value);
    }
    
    self::$table->focus();

    unset($data,$list,$len);

    endif;
  }


  public static function formatBytes($bytes, $precision = 2)
  {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');

    $bytes = max($bytes, 0);
    $pow   = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow   = min($pow, count($units) - 1);

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow];
  }


  public static function on_key( $key, $row, $event )
  {
    if( $key == 65288 ) {
      self::opendir( '..' );
    } elseif( $key == 65293 or $event->type == Gdk::_2BUTTON_PRESS ) {
      $filename = self::$table->get_values($row, 1);
      $ifdir = self::$path . $filename . DIRECTORY_SEPARATOR;
      if( is_dir($ifdir) ) {
        self::opendir( $filename );
      }
    }
  }
}
?>
